/*
 * @(#)XfslErrorDialog.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.view;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import xfuzzy.xfsl.view.listener.*;

import javax.swing.*;
import java.awt.*;

/**
 * Di�logo de selecci�n de pesos de la funci�n de error
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfslErrorDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603067L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Flag para indicar si elos pesos han sido seleccionados
	 */
	private boolean selected;
	
	/**
	 * Campos para introducir los pesos
	 */
	private XTextForm text[];
	
	/**
	 * Pesos
	 */
	private double weight[];

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslErrorDialog(JFrame frame, Variable[] out) {
		super((Frame) frame, "Xfsl", true);

		text = new XTextForm[out.length];
		weight = new double[out.length];
		for(int i=0; i<out.length; i++) text[i] = new XTextForm(out[i].getName());
		XTextForm.setWidth(text);

		String label[] = { "Set", "Cancel" };
		XCommandForm form = new XCommandForm(label,label, new XfslErrorActionListener(this) );
		form.setCommandWidth(100);

		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Output Weights"));
		for(int i=0; i<text.length; i++) content.add(text[i]);
		content.add(form);

		Point loc = frame.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		setLocation(loc);
		pack();
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Muestra la ventana con los valores indicados
	 */
	public boolean show(double[] value) {
		for(int i=0; i<text.length && i<value.length; i++)
			text[i].setText(""+value[i]);
		setVisible(true);
		return selected;
	}

	/**
	 * Obtiene los valores introducidos en los campos
	 */
	public double[] getWeights() {
		return weight;
	}

	/**
	 * Selecciona los valores de la ventana
	 */
	public void set() {
		if(!get()) XDialog.showMessage(null,"Values introduced are not correct");
		else { selected = true; setVisible(false); }
	}

	/**
	 * Cierra la ventana sin seleccionar los valores
	 */
	public void cancel() {
		selected = false;
		setVisible(false);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Lee los valores de los campos
	 */
	private boolean get() {
		boolean good = true;
		for(int i=0; i<text.length; i++) {
			boolean test = true;
			try { weight[i] = Double.parseDouble(text[i].getText()); }
			catch(NumberFormatException nfe) { test = false; }
			if(test && weight[i] <0) test = false;
			if(!test) { weight[i] = -1; text[i].setText(""); good = false;}
		}
		return good;
	}
}
